﻿//Copyright (C) <2013>  <jonathan cleeve norton> All Rights Reserved 
//Contact  jon.norton@fin-plus.co.uk website <http://www.fin-plus.co.uk/>
using System;
using FinPlusInterfaces;
using FinPlusAssembler;

namespace FinPlusCompCore
{
    public class FinPlusComponent : IFinPlusComp 
    {
        public virtual string Id { get; set; }
        public virtual string LinkedId { get; set; }
        public virtual string Config { get { return _config; } set { _config = string.IsNullOrEmpty(_config) ? value : _config; } }
        public virtual Env Env { get { return _env; } set { _env = _env == Env.unknown ? value : _env; } }
        public virtual IPods Adapter { get { return null; } }
        public virtual string TestString { get { return null; } }
        public virtual bool IsValid { get; set; }
        public string User { get { return _user ?? (_user = Environment.UserName); } }
        public IPods Commands { get { return _commands; } set { _commands = value; } }
        public IPPodManager Permissions
        {
            get
            {
                if (_permissionManager == null)
                    _permissionManager = IOC.Singleton<IPPodManager>("ServiceConfig", Config, Env);
                return _permissionManager;
            }
        }
        public IAuditManager Audits
        {
            get
            {
                if (_auditManager == null)
                    _auditManager = IOC.Singleton<IAuditManager>("ServiceConfig", User, Config, Env);
                return _auditManager;
            }
        }
        
        private static IPods _commands;
        private static Env _env = Env.unknown;
        private static IPPodManager _permissionManager;
        private static IAuditManager _auditManager;
        private static string _config, _user;
      
        //construct
        protected FinPlusComponent() { }

        //common control interface
        public virtual void Dock(object component, string dock) 
        {
            IsValid = false;
            throw new NotImplementedException();
        }

        public virtual void CmdRouter(object s)
        {
            try
            {
                CmdExecute((IPod)((IBean)s).Object);
            }
            catch (CmdRouterException e)
            {
                IsValid = Level.Error.Log(Id, Config, "", e);
            }
        }

        public virtual void CmdRouter(string command, string tag)
        {
            try
            {
                CmdExecute(IOC.New<IPod>(command, Id, User, -1, tag));
            }
            catch (CmdRouterException e)
            {
                IsValid = Level.Error.Log(Id, Config, command, e);
            }

        }

        public virtual void CmdRouter(string command)
        {
            try
            {
                CmdExecute(IOC.New<IPod>(command, Id, User));
            }
            catch (CmdRouterException e)
            {
                IsValid = Level.Error.Log(Id, Config, command, e);
            }
        }

        public virtual void CmdExecute(IPod command)
        {
            IsValid = false;
            throw new NotImplementedException();
        }

        public virtual void Dispose() { }

        //public
        public void CmdCapture(IPod command)
        {
            if (_commands == null || command["_CommandLogged"].ToString().Equals("True"))
                return;

            _commands.Add(command);

            command["_CommandLogged"] = "True";
        }
    }
}


